using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.DirectX.DirectDraw;
using TowerDefense_Dx.GameLogic;

namespace TowerDefense_Dx.Core
{
    public class DxAnimation
    {
        private DxSprite _sprite;
        public DxSprite Sprite
        {
            get { return _sprite;}
        }
        private double _frameTime;
        private double _ellapsedMilliSec = 0;

        private bool _isLoop;
        public bool IsLoop
        {
            get { return _isLoop; }
            set { _isLoop = value; }
        }

        private int _currentFrame = 1;
        public int CurrentFrame
        {   
            get { return _currentFrame; }
            set { _currentFrame = value; }
        }
        private int _firstFrame;
        private int _lastFrame;
        int tmpFirstFrame;
        int tmpLastFrame;
        Enemy.DIRECTION lastDirection;

        public DxAnimation(DxSprite sprite, float frameTime, bool isLoop)
        {
            this._sprite = sprite;
            this._frameTime = frameTime;
            this._isLoop = isLoop;
        }
        public DxAnimation(DxSprite sprite, float frameTime, int firstFrame,int lastFrame, bool isLoop)
        {
            this._sprite = sprite;
            this._frameTime = frameTime;
            this._isLoop = isLoop;
            this._firstFrame = firstFrame;
            this._currentFrame = _firstFrame;
            this._lastFrame = lastFrame;
        }
        public void Update(double deltaTime)
        {
            _ellapsedMilliSec += deltaTime;
            if (_ellapsedMilliSec > _frameTime)
            {
                _ellapsedMilliSec = 0;
                _currentFrame++;
                if (_currentFrame > _sprite.TotalFrame)
                {
                    if (_isLoop)
                    {
                        _currentFrame = 1;
                    }
                }
            }
        }
        public void Update(double deltaTime,  Enemy.DIRECTION direction )
        {
            if (direction != lastDirection)
            {
                switch (direction)
                {
                    case Enemy.DIRECTION.DOWN:
                        tmpFirstFrame = _firstFrame;
                        tmpLastFrame = _lastFrame;
                        _currentFrame = tmpFirstFrame;
                        lastDirection = direction;
                        break;
                    case Enemy.DIRECTION.LEFT:
                        tmpFirstFrame = _firstFrame + _sprite.Columns;
                        tmpLastFrame = _lastFrame + _sprite.Columns;
                        _currentFrame = tmpFirstFrame;
                        lastDirection = direction;
                        break;
                    case Enemy.DIRECTION.RIGHT:
                        tmpFirstFrame = _firstFrame + _sprite.Columns * 2;
                        tmpLastFrame = _lastFrame + _sprite.Columns * 2;
                        _currentFrame = tmpFirstFrame;
                        lastDirection = direction;
                        break;
                    case Enemy.DIRECTION.UP:
                        tmpFirstFrame = _firstFrame + _sprite.Columns * 3;
                        tmpLastFrame = _lastFrame + _sprite.Columns * 3;
                        _currentFrame = tmpFirstFrame;
                        lastDirection = direction;
                        break;
                }
            }
            _ellapsedMilliSec += deltaTime;
            if (_ellapsedMilliSec > _frameTime)
            {               
                _ellapsedMilliSec = 0;       
                _currentFrame++;
                if (_currentFrame > tmpLastFrame && _isLoop == true)
                {
                    _currentFrame = tmpFirstFrame;
                }                
            }
        }
        public void Draw(int x, int y, Surface destSurface)
        {
            _sprite.DrawFast(x, y, _currentFrame, destSurface, DrawFastFlags.Wait);
        }
	
    }
}
